Hard-coding bounds in FOR loops is a bad practice, just as magic numbers in general are. Often, those magic bounds can be replaced by dynamic
values. If that is not possible, replacing the literal number with a constant is still better.
Noncompliant code example
SET SERVEROUTPUT ON
DECLARE
TYPE myCollectionType IS VARRAY(3) OF VARCHAR2(42);
myCollection myCollectionType := myCollectionType('David', 'John', 'Richard');
BEGIN
FOR i IN 2 .. 3 -- Noncompliant; magic numbers used for the loop bounds
LOOP
DBMS_OUTPUT.PUT_LINE('name = ' || myCollection(i));
END LOOP;
FOR i IN 2 .. myCollection.LAST -- Noncompliant, better but still magic
LOOP
DBMS_OUTPUT.PUT_LINE('name = ' || myCollection(i));
END LOOP;
END;
/
Compliant solution
SET SERVEROUTPUT ON
DECLARE
TYPE myCollectionType IS VARRAY(3) OF VARCHAR2(42);
myCollection myCollectionType := myCollectionType('David', 'John', 'Richard');
BEGIN
FOR i IN myCollection.FIRST .. myCollection.LAST
LOOP
DBMS_OUTPUT.PUT_LINE('name = ' || myCollection(i));
END LOOP;
END;
/